// src/api/work.js
// src/api/work.js
import request from '@/utils/request'

// 基础路径
const BASE_URL = '/api/work'

/**
 * 获取作业详情
 * @param {string|number} id - 作业ID
 * @returns {Promise}
 */
export function getWorkInfo(id) {
  return request({
    url: `${BASE_URL}/${id}`,
    method: 'get'
  }).then(response => {
    // 可以在这里统一处理响应数据的格式转换
    const { data, code, message } = response
    if (code === 200) {
      return {
        code,
        data: {
          ...data,
          deadline: data.deadline || '',
          publishTime: data.publishTime || '',
          score: Number(data.score || 0),
          preventLateSubmit: Boolean(data.preventLateSubmit)
        },
        message
      }
    }
    throw new Error(message || '获取作业详情失败')
  })
}



/**
 * 上传作业文件
 * @param {FormData} formData - 包含文件的表单数据
 * @returns {Promise}
 */
export function uploadWorkFile(workId, formData) {
  return request({
    url: `/api/work/upload/${workId}`,
    method: 'post',
    data: formData,
    // 添加以下配置
    timeout: 30000, // 增加超时时间
    transformRequest: [(data) => data]
  })
}


/**
 * 更新作业信息
 * @param {Object} data - 作业数据
 * @returns {Promise}
 */
export function updateWork(id, data) {
  return request({
    url: `${BASE_URL}/update/${id}`,
    method: 'put',
    data: {
      id: data.id,
      courseId: data.courseId,
      type: data.type,
      title: data.title,
      content: data.content,
      activity: data.activity,
      chapterId: data.chapterId,
      section: data.section,
      releaseTime: data.releaseTime,
      endTime: data.endTime,
      score: data.score,
      isAllowOvertimeSubmission: data.isAllowOvertimeSubmission,
      file: data.file
    }
  })
}



/**
 * 删除作业
 * @param {string|number} id - 作业ID
 * @returns {Promise}
 */
export function deleteWork(workId) {
  return request({
    url: `/api/work/${workId}`,
    method: 'delete'
  })
}

export function getStudentWorks(params) {
  const { workId, status, page = 1, pageSize = 10 ,studentName } = params
  return request({
    url: `${BASE_URL}/stuwork/list`, 
    method: 'get',
    params: {
      workId,
      status,
      page,
      pageSize,
      studentName // 添加搜索参数
    }
  })
}

export function submitWorkGrade(data) {
  const { studentWorkId: id, score, message } = data  
  return request({
    url: `${BASE_URL}/stuwork/grade`,  
    method: 'post',
    params: {  
      id,
      score,
      message: message?.trim()
    }
  })
}

export function updateWorkStatus(id, status) {
  return request({
    url: `${BASE_URL}/stuwork/status`,  // 使用与其他API相同的路径格式
    method: 'post',
    params: {  
      id,
      status
    }
  })
}

export function getStudentWorkDetail(id) {
  return request({
    url: `/api/work/stuwork/detail/${id}`,
    method: 'get'
  })
}

export function downloadWorkFile(id) {
  return request({
    url: `/api/work/stuwork/download/${id}`,
    method: 'get',
    responseType: 'blob' , // 改为 blob
    headers: { // 添加合适的请求头
      'Accept': 'application/octet-stream'
    }
  })
}

export function expediteSubmission(id) {
  return request({
    url: `/api/work/stuwork/expedite/${id}`,
    method: 'post'
  })
}